package com.winning.sjzc.common.utils;

import java.util.List;

/**
 * @ClassName : Page
 * @Description : 分页工具类
 * @Author : zhanglei
 * @Date : 2019/6/11 17:16
 **/
public class Page<T> {
	public static int DEFAULT_PAGESIZE = 20;
	/**
	 * 总记录数
	 */
	private int totalNum;
	/**
	 * 每页显示条数
	 */
	private int pageSize = DEFAULT_PAGESIZE;
	/**
	 * 当前页
	 */
	private int pageCurrent;
	/**
	 * 总页数
	 */
	private int pageTotal;
	private String sortField;
	private String sortType;
	private List<T> list;

	public Page() {
		super();
	}

	public Page(int totalNum, int pageSize, int pageCurrent, List<T> list) {
		this.totalNum = totalNum;
		this.pageSize = pageSize;
		this.pageCurrent = pageCurrent;
		this.pageTotal = computePageTotal(totalNum, pageSize);
		this.list = list;
	}

	public Page(int totalNum, int pageSize, int pageCurrent, String sortField, String sortType, List<T> list) {
		this(totalNum, pageSize, pageCurrent, list);
		this.sortField = sortField;
		this.sortType = sortType;
	}

	public int getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(int totalNum) {
		this.totalNum = totalNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageCurrent() {
		return pageCurrent;
	}

	public void setPageCurrent(int pageCurrent) {
		this.pageCurrent = pageCurrent;
	}

	public int getPageTotal() {
		return pageTotal;
	}

	public void setPageTotal(int pageTotal) {
		this.pageTotal = pageTotal;
	}

	public String getSortField() {
		return sortField;
	}

	public void setSortField(String sortField) {
		this.sortField = sortField;
	}

	public String getSortType() {
		return sortType;
	}

	public void setSortType(String sortType) {
		this.sortType = sortType;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public static int computePageTotal(int totalNum, int pageSize) {
		if (totalNum < 1) {
			return 0;
		}

		if (pageSize == -1)
			return 1;

		if (pageSize < 1)
			throw new IllegalArgumentException("PageSize cann't be zero! Please check your initial define of Page." + pageSize);

		int size = (totalNum / pageSize);
		size = (totalNum % pageSize == 0) ? size : ++size;

		return size;
	}
}
